package com.jinyu.jyly.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.jinyu.jyly.R;
import com.jinyu.jyly.base.BaseActivity;
import com.jinyu.jyly.bean.AllInfoBean;
import com.jinyu.jyly.bean.LoginInfo;
import com.jinyu.jyly.global.GlobalConstants;
import com.jinyu.jyly.ui.MainActivity;
import com.jinyu.jyly.utils.AES;
import com.jinyu.jyly.utils.SharedPreferencesUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册
 */
public class RegisterActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {

    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_code)
    EditText etCode;
    @BindView(R.id.et_psw)
    EditText etPsw;
    @BindView(R.id.et_again_psw)
    EditText etAgainPsw;
    @BindView(R.id.btn_sub)
    Button btnSub;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;
    @BindView(R.id.checkbox)
    CheckBox checkbox;
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    private LoginPresenter presenter;
    private int mSmsId;

    @Override
    public int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected LoginPresenter getPresenter() {
        if (presenter == null) {
            presenter = new LoginPresenter(this, this);
        }
        return presenter;
    }

    @Override
    protected void initEventAndData() {
        QMUIStatusBarHelper.translucent(this);
        QMUIStatusBarHelper.setStatusBarDarkMode(this);

    }


    /**
     * 发送验证码
     *
     * @param infoBean
     */
    @Override
    public void onSendSms(AllInfoBean infoBean) {
        mTipDialog.dismiss();
        switch (infoBean.getStatus()) {
            case 200:
                try {
                    String data = infoBean.getData();
                    String result = AES.decrypt(data);
                    JSONObject jsonObject = new JSONObject(result);
                    mSmsId = jsonObject.optInt("id");
                    downTimer.start();
                    tvGetCode.setEnabled(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                tvGetCode.setEnabled(true);
                tvGetCode.setText("获取验证码");
                showToast(infoBean.getMsg());
                break;
            default:
                break;
        }
    }

    @Override
    public void onFailer(Throwable throwable) {
        mTipDialog.dismiss();
    }

    /**
     * 注册
     *
     * @param info
     */
    @Override
    public void onRegister(AllInfoBean info) {
        mTipDialog.dismiss();
        switch (info.getStatus()) {
            case 200:
                //
                Intent mLoginIntent = new Intent(this,LoginActivity.class);
                startActivity(mLoginIntent);
                finish();

                try {
                    String data = info.getData();
                    String result = AES.decrypt(data);
                    Gson gson = new Gson();
                    LoginInfo loginInfo = gson.fromJson(result, LoginInfo.class);
                    if(loginInfo != null){
                        String token = loginInfo.getToken();
                        SharedPreferencesUtils.setParam(this, GlobalConstants.LOGIN_TOKEN,token);

                        Intent mMainIntent = new Intent(this, MainActivity.class);
                        startActivity(mMainIntent);
                    }

                    Type listType = new TypeToken<ArrayList<String>>() {
                    }.getType();

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 422:
                showToast(info.getMsg());
                break;
            default:
                break;
        }
    }

    @Override
    public void onForgetPaw(AllInfoBean info) {
        //忘记密码
    }

    @Override
    public void onLogin(AllInfoBean info) {
        //kognshixian
    }


    @OnClick({R.id.tv_get_code, R.id.btn_sub,R.id.tv_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                String moblile = etMobile.getText().toString().trim();
                if (TextUtils.isEmpty(moblile)) {
                    showToast("手机号不能为空");
                    return;
                }

                boolean showing = mTipDialog.isShowing();
                if(!showing){
                    try {
                        mTipDialog.show();
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("mobile", moblile);
                        jsonObject.put("template", "register");
                        String pamam = AES.encryptToHexString(jsonObject.toString());
                        presenter.onSendSms(pamam);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.btn_sub:
                String moblil = etMobile.getText().toString().trim();
                String code = etCode.getText().toString().trim();
                String psw = etPsw.getText().toString().trim();
                String againPsw = etAgainPsw.getText().toString().trim();
                boolean checked = checkbox.isChecked();

                if (TextUtils.isEmpty(moblil)) {
                    showToast("手机号不能为空");
                    return;
                }
                if (TextUtils.isEmpty(code)) {
                    showToast("验证码不能为空");
                    return;
                }
                if (TextUtils.isEmpty(psw)) {
                    showToast("密码不能为空");
                    return;
                }

//                if(!checked){
//                    showToast("请勾选协议");
//                    return;
//                }

                boolean showingee = mTipDialog.isShowing();
                if(!showingee){
                    try {
                        mTipDialog.show();
                        //注册
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("mobile", moblil);
                        jsonObject.put("verycode", code);
                        jsonObject.put("smsid", mSmsId);
                        jsonObject.put("password", psw);
                        jsonObject.put("promotion", againPsw);
                        String pamam = AES.encryptToHexString(jsonObject.toString());
                        presenter.onRegister(pamam);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case R.id.tv_agreement:
                //协议

                break;
        }
    }


    CountDownTimer downTimer = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long arg0) {
            tvGetCode.setText("重新发送(" + arg0 / 1000 + ")");
            tvGetCode.setEnabled(false);
        }

        @Override
        public void onFinish() {
            tvGetCode.setEnabled(true);
            tvGetCode.setText("获取验证码");
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

}
